package com.wuxianedu.servive;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

/**
 * service 快速入门
 * Created by TED on 2016/9/21.
 */
public class MyService extends Service {


    private boolean flag;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }




    @Override
    public void onCreate() {
        Log.e("----------------", "-------onCreate---------");
        super.onCreate();

        new Thread() {
            @Override
            public void run() {
                flag = true;
                while (flag) {
                    Log.e("----", "耗时操作");
                    SystemClock.sleep(2000);
                }

            }
        }.start();


        // service会出现ANR

//        Log.e("---------",Thread.currentThread().getName()+"---------");
//        while(true){
//            Log.e("--------","-------后台要做的事------");
//            SystemClock.sleep(2000);
//        }


    }


    public void method(){
        Log.e("--------","-------method------");
        Toast.makeText(this,"service中的方法", Toast.LENGTH_SHORT).show();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("----------------", "-------onStartCommand---------");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        flag = false;
        Log.e("----------------", "-------onDestroy---------");
        super.onDestroy();
    }
}
